package site.assad.basicQuartzTest;

import java.util.Date;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import static org.quartz.DateBuilder.dateOf;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2018/1/19 17:02
 * Description: 演示 SimpleTriggerRunner
 */
public class SimpleTriggerRunner {
    public static void main(String[] args) throws SchedulerException, InterruptedException {

        //通过工厂方法创建Scheduler
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();

        //创建 JobDetail
        JobDetail jobDetail = newJob(HelloJob.class)           //指定实现类类型
                .withIdentity("job1","group1")   //指定identity
                .build();

        //创建 Trigger
        SimpleTrigger simpleTrigger =(SimpleTrigger)newTrigger()
                .withIdentity("trigger1","group1")  //指定 identity
                .startAt(new Date())                   //指定启动时间，如果为现在启动，使用.startNow()
                .withSchedule(simpleSchedule()
                            .withIntervalInSeconds(5)   //指定循环间隔时间，可以 withIntervalInMinutes，withIntervalInHours
                            .repeatForever())    //指定循环次数，无限循环，可以.withRepeatCount(200) 指定固定循环次数
                .endAt(dateOf(19,0,0)) //停止时间为当天的 19:00:00，可选
                .build();

        //在 scheduler 绑定 jobDetail 和 trigger
        scheduler.scheduleJob(jobDetail,simpleTrigger);
        scheduler.start();  //启动 scheduler


    }
}
